//
//  ByViewClassificationScroll.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/8.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewClassificationScroll.h"


@implementation ByCellClassificationItem
{
    UIView *_line;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    self.textLabel.font = FONT(14);
    self.textLabel.backgroundColor = [UIColor clearColor];
    self.textLabel.textColor = [UIColor blackColor];
    
    _line = [[UIView alloc] init];
    _line.backgroundColor = BY_LINE_COLOR;
    [self.contentView addSubview:_line];
    
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
 
    _line.frame = CGRectMake(0, 0, 0.5, self.height);
    _line.right = self.width;
}

- (void)setSelecting:(BOOL)selecting {
    
    _selecting = selecting;
    
    _line.hidden = _selecting;
    
    self.textLabel.textColor = _selecting ? [UIColor redColor] : [UIColor blackColor];
    self.backgroundColor = _selecting ? [UIColor clearColor] : [UIColor whiteColor];
}

@end


#pragma mark - 主视图
@implementation ByViewClassificationScroll
{
    NSInteger _selectingIndex;
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    if (self = [super initWithFrame:frame style:style]) {
        
        [self setupDefault];
    }
    return self;
}
// 创建表视图
- (void)setupDefault {
    
    _selectingIndex = -1;
    
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.separatorColor = BY_LINE_COLOR;
    self.delegate = self;
    self.dataSource = self;
    self.tableFooterView = [UIView new];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 0.5;
    [path moveToPoint:CGPointMake(self.width, 0)];
    [path addLineToPoint:CGPointMake(self.width, self.height)];
    [[UIColor lightGrayColor] setStroke];
    [path stroke];
}



#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.classModels.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellClassificationItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ByCellClassificationItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.selecting = (indexPath.row == _selectingIndex);
    
    ByModelClassification *model = self.classModels[indexPath.row];
    cell.textLabel.text = model.name;
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
// 分割线距离左边距离
-(void)viewDidLayoutSubviews
{
    if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
        [self setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
        [self setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}
// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 点击当前的
    if (_selectingIndex == indexPath.row) {
        return;
    }
    
    _selectingIndex = indexPath.row;
    
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    [self reloadData];
    
    if (self.selectBlock) {
        self.selectBlock(self.classModels[indexPath.row]);
    }
}

@end
